package com.useeinfo.oa.modules.base.entity;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.Configuration;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;

import javax.persistence.*;
import java.util.*;

/**
 * @Auther: xudong.sun
 * @Date: 2018/11/30 13:28
 * @Description: 设备资料
 */
@Entity
@Table(name = "facility_data")
public class FacilityData extends SuperClassEntity<FacilityData> {

    /**
     * 流水号
     */
    private String serialNum;

    /**
     * 设备资料名称
     */
    private String facilityDataName;

    /**
     * 设备资料描述简介
     */
    private String description;

    @ManyToOne
    @JoinColumn(name = "enterprise_id")
    private Enterprise enterprise;

    /**
     * 关联设备
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "facility_data_facility", joinColumns = {
            @JoinColumn(name = "facility_data_cat", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "facility_id", referencedColumnName = "id")})
    private Set<Facility> facilitySet = new HashSet<>();

    /**
     * 资料分类
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "facility_data_cat", joinColumns = {
            @JoinColumn(name = "facility_data_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "facility_cat_id", referencedColumnName = "id")})
    private Set<DataCat> dataCatSet = new HashSet<>();

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "facility_data_resource", joinColumns = {
            @JoinColumn(name = "facility_data_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "resource_id", referencedColumnName = "id")})
    private Set<Resource> resourceSet = new HashSet<Resource>();

    public String getSerialNum() {
        return serialNum;
    }

    public void setSerialNum(String serialNum) {
        this.serialNum = serialNum;
    }

    public String getFacilityDataName() {
        return facilityDataName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setFacilityDataName(String facilityDataName) {
        this.facilityDataName = facilityDataName;
    }

    public Set<Resource> getResourceSet() {
        return resourceSet;
    }

    public void setResourceSet(Set<Resource> resourceSet) {
        this.resourceSet = resourceSet;
    }

    public Set<Facility> getFacilitySet() {
        return facilitySet;
    }

    public void setFacilitySet(Set<Facility> facilitySet) {
        this.facilitySet = facilitySet;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public Set<DataCat> getDataCatSet() {
        return dataCatSet;
    }

    public void setDataCatSet(Set<DataCat> dataCatSet) {
        this.dataCatSet = dataCatSet;
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("serialNum", StringConverters.null2String(getSerialNum()));
        jsonObject.put("facilityDataName", StringConverters.null2String(getFacilityDataName()));
        jsonObject.put("description", StringConverters.null2String(getDescription()));
        if (getEnterprise() != null) {
            jsonObject.put("enterpriseName", StringConverters.null2String(getEnterprise().getEnterpriseName()));
        } else {
            jsonObject.put("enterpriseName", "");
        }
        jsonObject.put("createDate", DateTimeUtils.format(getCreateDate(), "yyyy-MM-dd HH:mm:ss"));

        List<String> facilities = new ArrayList<>();
        getFacilitySet().forEach(facility -> {
            facilities.add(facility.getName());
         });
        Collections.sort(facilities);

        List<String> dataCats = new ArrayList<>();
        getDataCatSet().forEach(dataCat -> {
            dataCats.add(dataCat.getDataCatName());
        });
        Collections.sort(dataCats);
        jsonObject.put("facilities", facilities);
        jsonObject.put("dataCats", dataCats);

        String filePath = "";
        String fileName = "";
        if (getResourceSet() != null && getResourceSet().size() != 0){
            List<Resource> resourceList = new ArrayList<>(getResourceSet());
            String hostPath = Configuration.getConfigurationByName("hostUrl");
            hostPath += hostPath.endsWith("/") ? "" : "/";
            filePath = hostPath + resourceList.get(0).getPath();
            fileName = resourceList.get(0).getResourceName();
        }
        jsonObject.put("filePath", filePath);
        jsonObject.put("fileName", fileName);
        return jsonObject;
    }
}
